package com.activity.drag.drop;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ActivityDragDropActivity extends ListActivity {

	ArrayList allContents = null;
	ArrayList<String> rcontent = null;
	ArrayList<String> lcontent = null;
	ArrayList<Integer> images;
	HashMap<String, String> answers = null;
	//Called when submit button is clicked
	public void onClickSubmit(View v) {
		//check if the item match and update the image
		for (int i = 0; i < lcontent.size(); i++) {
			String temp = answers.get(lcontent.get(i));
			if (temp.equalsIgnoreCase(rcontent.get(i))) {
				images.add(i, R.drawable.tick2);
			} else {
				images.add(i, R.drawable.wrong);
				Toast.makeText(getApplicationContext(),
						"You are close.Try again", Toast.LENGTH_SHORT).show();
			}
		}
		setListAdapter(new DragDropAdapter(this,
				new int[] { R.layout.items }, new int[] { R.id.TextView01 },allContents));
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dragdroplistview);
		Toast.makeText(getApplicationContext(),
				"Match definitions with appropriate left items", Toast.LENGTH_SHORT).show();

		//populate data into arraylist
		populateData();
		//Set the data to list adapter
		setListAdapter(new DragDropAdapter(this,
				new int[] { R.layout.items }, new int[] { R.id.TextView01 },allContents));
		
		ListView listView = getListView();

		if (listView instanceof DragDropListview) {
			((DragDropListview) listView).setDropListener(mDropListener);
			((DragDropListview) listView).setRemoveListener(mRemoveListener);
			((DragDropListview) listView).setDragListener(mDragListener);
		}

	}

	private void populateData() {
		rcontent = new ArrayList<String>(mListOptions.length);
		for (int i = 0; i < mListOptions.length; i++) {
			rcontent.add(mListOptions[i]);
		}

		lcontent = new ArrayList<String>(mListItems.length);
		for (int i = 0; i < mListItems.length; i++) {
			lcontent.add(mListItems[i]);
		}

		images = new ArrayList<Integer>();
		for (int i = 0; i < mListOptions.length; i++) {
			images.add(R.drawable.icmpmove);
		}

		answers = new HashMap<String, String>();
		answers.put("1.Deposition",
				"Sediments are laid down. Over time they create layered landforms.");
		answers.put(
				"2.Weathering",
				"Physical and chemical break down of rocks into smaller pieces that create sediments.");
		answers.put(
				"3.Erosion   ",
				"The physical transportation or movement of weathered or broken down rock materials by water, wind and gravity.");

		allContents = new ArrayList();
		allContents.add(lcontent);
		allContents.add(rcontent);
		allContents.add(images);
		allContents.add(answers);
	}

	private DropListener mDropListener = new DropListener() {
		public void onDrop(int from, int to) {
			ListAdapter adapter = getListAdapter();
			if (adapter instanceof DragDropAdapter) {
				((DragDropAdapter) adapter).onDrop(from, to);
				getListView().invalidateViews();
			}
		}
	};

	private RemoveListener mRemoveListener = new RemoveListener() {
		public void onRemove(int which) {
			ListAdapter adapter = getListAdapter();
			if (adapter instanceof DragDropAdapter) {
				((DragDropAdapter) adapter).onRemove(which);
				getListView().invalidateViews();
			}
		}
	};

	private DragListener mDragListener = new DragListener() {
		int backgroundColor = Color.TRANSPARENT;//BLUE; //0xe0103010;
		int defaultBackgroundColor;

		public void onDrag(int x, int y, ListView listView) {
			// TODO Auto-generated method stub
		}

		public void onStartDrag(View itemView) {
			itemView.setVisibility(View.INVISIBLE);
			defaultBackgroundColor = itemView.getDrawingCacheBackgroundColor();
			itemView.setBackgroundColor(backgroundColor);
			ImageView iv = (ImageView) itemView.findViewById(R.id.ImageView01);
			if (iv != null)
				iv.setVisibility(View.INVISIBLE);
		}

		public void onStopDrag(View itemView) {
			itemView.setVisibility(View.VISIBLE);
			itemView.setBackgroundColor(defaultBackgroundColor);
			ImageView iv = (ImageView) itemView.findViewById(R.id.ImageView01);
			if (iv != null)
				iv.setVisibility(View.VISIBLE);
		}

	};
	private static String[] mListItems = { "1.Deposition", "2.Weathering",
			"3.Erosion   " };
	private static String[] mListOptions = {
			"The physical transportation or movement of weathered or broken down rock materials by water, wind and gravity.",
			"Sediments are laid down. Over time they create layered landforms.",
			"Physical and chemical break down of rocks into smaller pieces that create sediments." };

}